<?php
session_start();
require 'lib/lib/GooglePieChart.php';

include_once("../../app/dao/database.php");

$class_db = new Database;

$query = "SELECT product_detail.product_name as product, SUM(order_detail.total) as total FROM product_detail 
        INNER JOIN order_detail ON product_detail.productdet_id = order_detail.product 
        WHERE order_detail.store=$_SESSION[id_toko] GROUP BY product DESC LIMIT 0,5";

$hasil = $class_db->select_table($query);
//print_r($hasil);

$values = array();
$produk = array();

foreach($hasil as $data){
    $values[] = $data['total'];    
    $produk[] = $data['product'];
}

$total = 0;

for($i=0;$i<count($values);$i++){
    $total += $values[$i];
}

for($i=0;$i<count($values);$i++){
    $values[$i]= ($values[$i]/$total)*100;
}

//print_r($values);
//print_r($produk);
$chart1 = new GooglePieChart('p', 500, 200);
//$chart->addData(new GoogleChartData(array(10,20,30)));

$data1 = new GoogleChartData($values);
$chart1->addData($data1);
		
$chart1->setQueryMethod(GoogleChartApi::GET);
$data1->setLabels($produk);

if ( isset($_GET['debug']) ) {
	var_dump($chart1->getQuery());
	echo $chart1->validate();
	echo $chart1->toHtml();
}
else {
	header('Content-Type: image/png');
	echo $chart1;
}
